package cn.dida.controller;

import cn.dida.service.SalesmanCashService;
import com.alibaba.fastjson.JSONObject;

import cn.dida.model.HttpResponseBodyWrapper;
import cn.dida.service.ActivityWinningService;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.UnsupportedEncodingException;
import java.util.Map;


@Controller

public class CustmoerCallbackController extends BaseController  {
	private Log loger = LogFactory.getLog(CustmoerCallbackController.class);
	@Autowired
	private ActivityWinningService activitywinService;

	@Autowired
	private SalesmanCashService cashService;

	@RequestMapping(value = "/customerredbagscallback", produces="application/json;charset=UTF-8")
	@ResponseBody
	public HttpResponseBodyWrapper updateCustomerReBagsState(@RequestBody String jsonString,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException { 
    	JSONObject js = JSONObject.parseObject(jsonString);
    	if(js.containsKey("message_id")){
    		//通过messageid更新红包发送状态
    		String message_id  = js.getString("message_id");
    		Integer payresult = js.getInteger("pay_result");
    		String descripe = js.getString("descripe");
    		activitywinService.calbackupdateActivitywinningsendredlog(message_id, payresult == 0 ? 1 : 0,descripe);
    		Map<String, Object> resultmap = activitywinService.queryMessage_Activity_winning_ID(message_id);
    		if(resultmap!=null){
    			Integer actvity_winning_id = (Integer) resultmap.get("actvity_winning_id");
    			Integer activity_prize_id = (Integer) resultmap.get("activity_prize_id");
    			if( payresult == 0 ){
    				// 红包发送成功，修改兑奖记录，增加兑奖数量，减少库存，
    				activitywinService.updateActWinningCashSta(actvity_winning_id, activity_prize_id,1);
    			}else{
    				activitywinService.updateActWinningCashSta(actvity_winning_id, activity_prize_id,0);
    			}
    		}
    	}
    	//boolean result = salesmanService.updatePaymentState(js);
//		{"message_tag":"REDPACK","message_id":"0A97CB6600002A9F00000011EACD6551","id":630,"descripe":"","req_count":1,"pay_result":0}
    	loger.debug("*****************Marketing*******************************");
    	loger.debug(jsonString);
    	loger.debug("*****************Marketing*****************************");
		return new HttpResponseBodyWrapper(true);

	}

	/**
	 * 促销员支付回调
	 * @param jsonString
	 * @param request
	 * @param response
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/salesmanPayCallback", produces="application/json;charset=UTF-8")
	@ResponseBody
	public HttpResponseBodyWrapper salesmanPayCallback(@RequestBody String jsonString,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
		JSONObject js = JSONObject.parseObject(jsonString);
		if(js.containsKey("message_id")){
			//通过messageid更新支付状态
			String message_id  = js.getString("message_id");
			Integer payresult = js.getInteger("pay_result"); //0、支付成功
			String descripe = js.getString("descripe");
			cashService.update_callback(payresult,message_id,descripe);
		}
		loger.debug("*****************Marketing*******************************");
		loger.debug(jsonString);
		loger.debug("*****************Marketing*****************************");
		return new HttpResponseBodyWrapper(true);

	}
}
